<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Processimage extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    private function _get_image_extension($filename, $include_dot = false, $shorter_extensions = true) {
        $image_info = @getimagesize($filename);
        if (!$image_info || empty($image_info[2])) {
            return false;
        }
        $extension = image_type_to_extension($image_info[2], $include_dot);
        if (!$extension) {
            return false;
        }

        if ($shorter_extensions) {
            $replacements = array(
                'jpeg' => 'jpg',
                'tiff' => 'tif',
            );
            $extension = strtr($extension, $replacements);
        }
        return $extension;
    }

    private function _joinimage($background, $avt_tpl, $cover, $avt, $file_ext, $userid) {
        $background_img = imagecreatefrompng($background);
        $avt_tpl_img = imagecreatefrompng($avt_tpl);
        if ($file_ext == 'jpg') {
            $avt_img = imagecreatefromjpeg($avt);
            $cover_img = imagecreatefromjpeg($cover);
        } else if ($file_ext == 'png') {
            $avt_img = imagecreatefrompng($avt);
            $cover_img = imagecreatefrompng($cover);
        } else if ($file_ext == 'gif') {
            $avt_img = imagecreatefromgif($avt);
            $cover_img = imagecreatefromgif($cover);
        } else {
            return null;
        }
        imagecopyresampled($background_img, $cover_img, 0, 0, 0, 0, 851, 315, 851, 315);
        imagecopyresampled($background_img, $avt_tpl_img, 25, 198, 0, 0, 164, 164, 164, 164);
        imagecopyresampled($background_img, $avt_img, 27, 200, 0, 0, 160, 160, 160, 160);

        $join_file = $userid . '_preview_' . '.png';
        imagepng($background_img, 'images/crop/' . $join_file, 9);
        return $join_file;
    }

    function uploadImage() {
        $config = array();
        $config['upload_path'] = 'uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '1500';
        $config['min_width'] = '1024';
        $config['min_height'] = '768';
        $config['max_height'] = '1000';
        $config['file_name'] = $this->facebook->getUser()."_upload";
        $config['overwrite'] = false;

        $this->load->library('upload', $config);
        //header('Content-type: text/json');
        //header('Content-type: application/json');
        if (!$this->upload->do_upload('image_file')) {
            $result = array("message" => 0,'error' => strip_tags($this->upload->display_errors()));
        } else {
            $data = $this->upload->data();
            $result = array("message" => 1,'upload_data' => $data['file_name']);
        }
        echo json_encode($result);
    }

    function zipFiles($outputname, $listfiles) {
        $zip = new ZipArchive(); // Load zip library
        $zip_name = $outputname; // Zip name
        if ($zip->open($zip_name, ZIPARCHIVE::OVERWRITE) !== TRUE) {
            // Opening zip file to load files
            $error .= "* Sorry ZIP creation failed at this time";
            return false;
        }
        foreach ($listfiles as $file) {
            $zip->addFile($file); // Adding files into zip
        }
        $zip->close();
        return true;
    }

    function process() {
        /* $_REQUEST['userid']='userid';
          $_REQUEST['url']='images/source.jpg';
          $_REQUEST['x']=0;
          $_REQUEST['y']=100;
         */
        $background = 'images/Facebook-Timeline-Template Cover.png';
        $avt_tpl = 'images/Facebook-Timeline-Template avt.png';
		$file_ext='';
        if (isset($_REQUEST['userid']) && isset($_REQUEST['url']) && isset($_REQUEST['x']) && isset($_REQUEST['y'])) {
            $userid = $_REQUEST['userid'];
            $url = $_REQUEST['url'];
            $x = $_REQUEST['x'];
            $y = $_REQUEST['y'];
            $file_ext = $this->_get_image_extension($url);
            $newfilename = $userid . '.' . $file_ext;
            $img = 'images/crop/' . $newfilename;
            $percent = 851 / 755;
            // file zip name
            $outputzip = 'images/crop/covereasydesign_' . $userid . '.zip';

            file_put_contents($img, file_get_contents($url));
            //resize image
            $sizeimg = getimagesize($img);
            $src_width = $sizeimg[0];
            $src_height = $sizeimg[1];
            $percent_resize = 851 / $src_width;
            if ($file_ext == 'jpg') {

                // scale
                // size of image after resize
                $dest_width = $src_width * $percent_resize;
                $dest_height = $src_height * $percent_resize;

                $resizeimage_tmp = imagecreatetruecolor($dest_width, $dest_height);
                $src_img_tmp = imagecreatefromjpeg($img);
                $resizefilename = 'resized_' . $userid . '.' . $file_ext;
                imagecopyresampled($resizeimage_tmp, $src_img_tmp, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
                imagejpeg($resizeimage_tmp, 'images/crop/' . $resizefilename, 100);
                // end resize
                // crop imp
                $src_img = imagecreatefromjpeg('images/crop/' . $resizefilename);
                //create cover
                $x = $x * $percent;
                $y = $y * $percent;
                $cover_img = imagecreatetruecolor(851, 315);
                imagecopy($cover_img, $src_img, 0, 0, $x, $y, 851, 315);
                $cover_file = $userid . '_cover.' . $file_ext;
                imagejpeg($cover_img, 'images/crop/' . $cover_file, 100);
                //create avatar
                $dest_img = imagecreatetruecolor(160, 160);
                $x_status = $x + 27;
                $y_status = $y + 200;
                imagecopy($dest_img, $src_img, 0, 0, $x_status, $y_status, 160, 160);
                $avartar_file_tmp = $userid . '_avatar_tmp' . '.' . $file_ext;
                imagejpeg($dest_img, 'images/crop/' . $avartar_file_tmp, 100);
                $avatar = imagecreatefromjpeg('images/crop/' . $avartar_file_tmp);

                $resize_avatar = imagecreatetruecolor(180, 180);

                imagecopyresampled($resize_avatar, $avatar, 0, 0, 0, 0, 180, 180, 160, 160);
                $avartar_file = $userid . '_avatar' . '.' . $file_ext;
                imagejpeg($resize_avatar, 'images/crop/' . $avartar_file, 100);
                $filePreview = $this->_joinimage($background, $avt_tpl, 'images/crop/' . $cover_file, 'images/crop/' . $avartar_file_tmp, 'jpg', $userid);
                //create zip
                $listfiles = array('images/crop/' . $avartar_file, 'images/crop/' . $cover_file);
                $this->zipFiles($outputzip, $listfiles);
            } else if ($file_ext == 'png') {
                // size of image after resize
                $dest_width = $src_width * $percent_resize;
                $dest_height = $src_height * $percent_resize;

                $resizeimage_tmp = imagecreatetruecolor($dest_width, $dest_height);
                $src_img_tmp = imagecreatefrompng($img);
                $resizefilename = 'resized_' . $userid . '.jpg';
                imagecopyresampled($resizeimage_tmp, $src_img_tmp, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
                imagejpeg($resizeimage_tmp, 'images/crop/' . $resizefilename, 100);
                // end resize
                // crop imp
                $src_img = imagecreatefromjpeg('images/crop/' . $resizefilename);
                //create cover
                $x = $x * $percent;
                $y = $y * $percent;
                $cover_img = imagecreatetruecolor(851, 315);
                imagecopy($cover_img, $src_img, 0, 0, $x, $y, 851, 315);
                $cover_file = $userid . '_cover.' . $file_ext;
                imagejpeg($cover_img, 'images/crop/' . $cover_file, 100);
                //create avatar
                $dest_img = imagecreatetruecolor(160, 160);
                $x_status = $x + 27;
                $y_status = $y + 200;
                imagecopy($dest_img, $src_img, 0, 0, $x_status, $y_status, 160, 160);
                $avartar_file_tmp = $userid . '_avatar_tmp' . '.' . $file_ext;
                imagejpeg($dest_img, 'images/crop/' . $avartar_file_tmp, 100);
                $avatar = imagecreatefromjpeg('images/crop/' . $avartar_file_tmp);

                $resize_avatar = imagecreatetruecolor(180, 180);

                imagecopyresampled($resize_avatar, $avatar, 0, 0, 0, 0, 180, 180, 160, 160);
                $avartar_file = $userid . '_avatar' . '.' . $file_ext;
                imagejpeg($resize_avatar, 'images/crop/' . $avartar_file, 100);
                $filePreview = $this->_joinimage($background, $avt_tpl, 'images/crop/' . $cover_file, 'images/crop/' . $avartar_file_tmp, 'jpg', $userid);
                //create zip
                $listfiles = array('images/crop/' . $avartar_file, 'images/crop/' . $cover_file);
                $this->zipFiles($outputzip, $listfiles);
            } else if ($file_ext == 'gif') {
                // size of image after resize
                $dest_width = $src_width * $percent_resize;
                $dest_height = $src_height * $percent_resize;

                $resizeimage_tmp = imagecreatetruecolor($dest_width, $dest_height);
                $src_img_tmp = imagecreatefromgif($img);
                $resizefilename = 'resized_' . $userid . '.' . $file_ext;
                imagecopyresampled($resizeimage_tmp, $src_img_tmp, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
                imagegif($resizeimage_tmp, 'images/crop/' . $resizefilename);
                // end resize
                // crop imp
                $src_img = imagecreatefromgif('images/crop/' . $resizefilename);
                //create cover
                $cover_img = imagecreatetruecolor(851, 315);
                imagecopy($cover_img, $src_img, 0, 0, $x, $y, 851, 315);
                $cover_file = $userid . '_cover_' . '.' . $file_ext;
                imagegif($cover_img, 'images/crop/' . $cover_file);
                //create avatar
                $dest_img = imagecreatetruecolor(160, 160);
                $x_status = $x * $percent + 27;
                $y_status = $y * $percent + 200;
                imagecopy($dest_img, $src_img, 0, 0, $x_status, $y_status, 160, 160);
                $avartar_file_tmp = $userid . '_avatar_tmp' . '.' . $file_ext;
                imagegif($dest_img, 'images/crop/' . $avartar_file_tmp);
                $avatar = imagecreatefromgif('images/crop/' . $avartar_file_tmp);

                $resize_avatar = imagecreatetruecolor(180, 180);

                imagecopyresampled($resize_avatar, $avatar, 0, 0, 0, 0, 180, 180, 160, 160);
                $avartar_file = $userid . '_avatar' . '.' . $file_ext;
                imagegif($resize_avatar, 'images/crop/' . $avartar_file);
                $filePreview = $this->_joinimage($background, $avt_tpl, 'images/crop/' . $cover_file, 'images/crop/' . $avartar_file_tmp, 'gif', $userid);
                // create zip
                $listfiles = array('images/crop/' . $avartar_file, 'images/crop/' . $cover_file);
                $this->zipFiles($outputzip, $listfiles);
            }
        } else {
            $filePreview = null;
        }
        $msg = array("msg" => 1,'ext'=>$file_ext);
        header('Content-Type: application/json');
        echo json_encode($msg);
        return $filePreview;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */